package com.saile.controller.web.pioneer.media;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saile.controller.base.BaseController;
import com.saile.def.Constant;
import com.saile.domain.Media;
import com.saile.domain.ViewsQuantity;
import com.saile.result.ResultObj;
import com.saile.service.AdvertService;
import com.saile.service.MediaService;
import com.saile.service.MessageBoardService;
import com.saile.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/14.
 * 先锋号controller
 */
@RequestMapping("/pioneer/media")
@Controller
public class MediaController extends BaseController {
    @Autowired
    AdvertService advertService;
    @Autowired
    NewsService newsService;
    @Autowired
    MediaService mediaService;
    @Autowired
    MessageBoardService messageBoardService;

    /**
     * 媒体列表页面
     *
     * @param type 1：视频，2：音频
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index(Byte type) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/media/voice");

        if(type==null){
            type=Media.MEDIA_TYPE_VOICE;
        }
        if (type == Media.MEDIA_TYPE_VIDEO) {
            mov.setViewName("/web/pioneer/media/video");
        }
        mov.addObject("mediaType",type);
        return mov;
    }

    @RequestMapping(value = "/playVideo")
    public ModelAndView playVideo(Long tid) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/media/playVideo");
        Map<String, String> map = new HashMap<>();
        map.put("tid", String.valueOf(tid));
        Media media = mediaService.getMedia(map);
        List<Media> mediaList = mediaService.recommendVideo();
        mov.addObject("media", media);
        mov.addObject("mediaList", mediaList);
        addViewsQuantity(tid, ViewsQuantity.RESOURCE_TYPE_MEDIA);
        return mov;
    }

    @RequestMapping(value = "/playVoice")
    public ModelAndView playVoice(Long tid) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/media/playVoice");
        Map<String, String> map = new HashMap<>();
        map.put("tid", String.valueOf(tid));
        Media media = mediaService.getMedia(map);
        map.clear();
        map.put("albumId",String.valueOf(media.getAlbumId()));
        List<Media>  mediaList=mediaService.getMediaList(map);
        mov.addObject("media", media);
        mov.addObject("mediaList", mediaList);
        addViewsQuantity(tid, ViewsQuantity.RESOURCE_TYPE_MEDIA);
        return mov;
    }

    /**
     * 获取新闻列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getMediaList")
    @ResponseBody
    public String getMediaList(Integer page, Integer pageSize) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            if (page == null || page == 0) {
                page = Constant.PAGENUM;
            }
            // 设置每页记录数
            if (pageSize == null || pageSize == 0) {
                pageSize = Constant.PAGESIZENum;
            }
            Map<String, String> map = super.showRequestParameters();
            map.put("page", String.valueOf(page));
            map.put("pageSize", String.valueOf(pageSize));
            PageHelper.startPage(page, pageSize);
            List<Media> mediaList = mediaService.getMediaList(map);
            PageInfo<Media> pageinfo = new PageInfo<Media>(mediaList);
            resultObj.setData(pageinfo);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


    /**
     * 猜你喜欢
     *
     * @return
     */
    @RequestMapping(value = "/guessYouLike")
    public ModelAndView guessYouLike() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/media/guessYouLike");
        return mov;
    }

    /**
     * 精选队课
     *
     * @return
     */
    @RequestMapping(value = "/videoRecommend")
    public ModelAndView videoRecommend() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/media/videoRecommend");

        Map<String, String> map = new HashMap<>();
        map.put("mediaType", String.valueOf(Media.MEDIA_TYPE_VIDEO));
        map.put("length", String.valueOf(8));
        List<Media> mediaList = mediaService.getGuessYouLike(map);
        mov.addObject("mediaList", mediaList);
        return mov;
    }

    /**
     * 大家都爱听
     *
     * @return
     */
    @RequestMapping(value = "/voiceRecommend")
    public ModelAndView voiceRecommend() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/web/pioneer/media/voiceRecommend");
        Map<String, String> map = new HashMap<>();
        map.put("mediaType", String.valueOf(Media.MEDIA_TYPE_VOICE));
        map.put("length", String.valueOf(5));
        List<Media> mediaList = mediaService.getGuessYouLike(map);
        mov.addObject("mediaList", mediaList);
        return mov;
    }


    /**
     * 获猜你喜欢
     *
     * @return
     */
    @RequestMapping(value = "/getGuessYouLike")
    @ResponseBody
    public String getGuessYouLike(Byte mediaType, Integer length) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {


            Map<String, String> map = new HashMap<>();
            map.put("mediaType", String.valueOf(mediaType));
            map.put("length", String.valueOf(length));
            List<Media> mediaList = mediaService.getGuessYouLike(map);

            resultObj.setData(mediaList);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


}
